class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid):
        max_col = len(obstacleGrid[0])
        max_row = len(obstacleGrid)
        obstacleGrid[-1][-1] = 0 if obstacleGrid[-1][-1] else 1
        # 初始化下底边和右边
        for i in range(max_col-2, -1, -1):
            obstacleGrid[-1][i] = 0 if obstacleGrid[-1][i] else obstacleGrid[-1][i+1] 
        for i in range(max_row-2, -1, -1):
            obstacleGrid[i][-1] = 0 if obstacleGrid[i][-1] else obstacleGrid[i+1][-1] 

        for row in range(max_row-2, -1, -1):
            for col in range(max_col-2, -1, -1):
                if not obstacleGrid[row][col]:
                    # 当前格无障碍，则汇总下、右两格总路径数
                    obstacleGrid[row][col] += obstacleGrid[row][col+1]
                    obstacleGrid[row][col] += obstacleGrid[row+1][col]
                else:
                    # 当前格有障碍，路径数置为0
                    obstacleGrid[row][col] = 0
        return obstacleGrid[0][0]